package com.see.front.view.staticdata;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.see.domain.dao.DataNucleusUtil;
import com.see.domain.model.StaticData;
import com.see.front.data.StaticDataAdaptor;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.event.Action;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.CellStyleGenerator;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
public class TableLayout extends VerticalLayout {

    static Table                    table            = new Table("");

    HashSet<Object>                 markedRows       = new HashSet<Object>();

    static final Action             ACTION_MARK      = new Action("Mark");
    static final Action             ACTION_UNMARK    = new Action("Unmark");
    static final Action             ACTION_LOG       = new Action("Save");

    static final Action[]           ACTIONS_UNMARKED = new Action[] {ACTION_MARK, ACTION_LOG};
    static final Action[]           ACTIONS_MARKED   = new Action[] {ACTION_UNMARK, ACTION_LOG};

    private static IndexedContainer container;

    public TableLayout() {
        addComponent(table);

        // Label to indicate current selection
        final Label selected = new Label("No selection");
        addComponent(selected);

        // set a style name, so we can style rows and cells
        table.setStyleName("iso3166");

        // size
        table.setWidth("100%");
        table.setHeight("500px");

        // selectable
        table.setSelectable(true);
        table.setMultiSelect(true);
        table.setImmediate(true); // react at once when something is selected

        loadFromDataSource(null, null, null);

        // turn on column reordering and collapsing
        table.setColumnReorderingAllowed(true);
        table.setColumnCollapsingAllowed(true);

        // set column headers
        table.setColumnHeaders(new String[] {"Symbol", "Sector", "Industry"});

        // Column alignment
        table.setColumnAlignment(StaticDataAdaptor.TITLE_SECTOR, Table.ALIGN_CENTER);

        // Column width
        table.setColumnExpandRatio(StaticDataAdaptor.TITLE_SYMBOL, 1);
        table.setColumnWidth(StaticDataAdaptor.TITLE_SYMBOL, 70);
        table.setColumnWidth(StaticDataAdaptor.TITLE_SECTOR, 100);

        // Actions (a.k.a context menu)
        table.addActionHandler(new Action.Handler() {
            public Action[] getActions(Object target, Object sender) {
                if (markedRows.contains(target)) {
                    return ACTIONS_MARKED;
                } else {
                    return ACTIONS_UNMARKED;
                }
            }

            public void handleAction(Action action, Object sender, Object target) {
                if (ACTION_MARK.equals(action)) {
                    markedRows.add(target);
                    table.requestRepaint();
                } else if (ACTION_UNMARK.equals(action)) {
                    markedRows.remove(target);
                    table.requestRepaint();
                } else if (ACTION_LOG.equals(action)) {
                    Item item = table.getItem(target);
                    addComponent(new Label("Saved: " + target + ", "
                        + item.getItemProperty(StaticDataAdaptor.TITLE_SYMBOL).getValue()));
                }

            }

        });

        // style generator
        table.setCellStyleGenerator(new CellStyleGenerator() {
            public String getStyle(Object itemId, Object propertyId) {
                if (propertyId == null) {
                    // no propertyId, styling row
                    return (markedRows.contains(itemId) ? "marked" : null);
                } else if (StaticDataAdaptor.TITLE_SYMBOL.equals(propertyId)) {
                    return "bold";
                } else {
                    // no style
                    return null;
                }

            }

        });

        // listen for valueChange, a.k.a 'select' and update the label
        table.addListener(new Table.ValueChangeListener() {
            public void valueChange(ValueChangeEvent event) {
                // in multiselect mode, a Set of itemIds is returned,
                // in singleselect mode the itemId is returned directly
                Set< ? > value = (Set< ? >) event.getProperty().getValue();
                if (null == value || value.size() == 0) {
                    selected.setValue("No selection");
                } else {
                    selected.setValue("Selected: " + table.getValue());
                }
            }
        });

    }

    public static void loadFromDataSource(String symbol, String sector, String industry) {
        
        if (sector != null && sector.length() > 0) {
            
            container = StaticDataAdaptor.getSectorContainer(sector);
            table.setContainerDataSource(container);
        }
        else  if (industry != null && industry.length() > 0) {
            container = StaticDataAdaptor.getIndustryContainer(industry);
            table.setContainerDataSource(container);

        } else {
            container = StaticDataAdaptor.getSymbolContainer(symbol);
            table.setContainerDataSource(container);
            
        }
        

    }

   
}
